﻿using System;
using System.Collections.Generic;
using System.IO;
using Contracts.Entities;
using NLog;

namespace Definition.Import
{
    public class ReadMovieItemsFromDirectory
    {
        protected Logger logger = LogManager.GetCurrentClassLogger();

        private List<MovieItem> movieItems;

        public ReadMovieItemsFromDirectory()
        {
            movieItems = new List<MovieItem>();
        }

        public void Process(string directoryPath)
        {
            logger.Debug(String.Format("Try Read From Directory path {0}", directoryPath));
      
            foreach (var directory in Directory.GetDirectories(directoryPath))
            {
                var directoryInfo = new DirectoryInfo(directory);

                if (directoryInfo.GetDirectories().Length > 0)
                    Process(directory);
                else
                    movieItems.Add(new MovieItem() { Name = directoryInfo.Name, FolderPath = directory });
            }

            OnCompleted(movieItems);
        }


        public event Action<List<MovieItem>> OnCompleted = delegate { };
    }
}
